Yes.
When Fib(N)
is computed recursively, very many activations are created
and destroyed.
Sometimes the time it takes to compute Fib(N)
is used as a
benchmark, a program that tests the speed of a computer.
Here is a bare-minimum program for Fib(N)
:
class FibonacciCalc { public int Fib( int N ) { if ( N==1 ) return 1; else if ( N==2 ) return 1; else return Fib( N-1 ) + Fib( N-2 ); } } class FibonacciTester { public static void main ( String[] args) { int argument = Integer.parseInt( args[0] ); FibonacciCalc f = new FibonacciCalc(); int result = f.Fib( argument ); System.out.println("Fib(" + argument + ") is " + result ); } }
Here are some results of running the program on my IBM ThinkPad 380ED. You might wish to run the program on your computer and compare speeds.
N | 10 | 20 | 30 | 35 | 40 | 45 |
---|---|---|---|---|---|---|
Fib(N) | 55 | 6765 | 832040 | 9227465 | 102334155 | 1134903170 |
time (sec) | 2 | 2 | 3 | 4 | 30 | 360 |
It takes a few seconds for the Java system to load and start running. This time is included in these measurements.